package com.skb.web.controller;

import com.skb.core.model.CommissionRate;
import com.skb.core.model.InterestRate;
import com.skb.core.model.Segment;
import com.skb.core.model.controller.CommissionRateModel;
import com.skb.core.model.controller.InterestRateModel;
import com.skb.core.model.controller.SegmentModel;
import com.skb.core.service.ICommissionRateService;
import com.skb.core.service.IInterestRateService;
import com.skb.core.service.ISegmentService;
import com.skb.core.service.IUserService;
import com.skb.utils.JsonPagedDataAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SegmentController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ISegmentService segmentService;

    @Autowired
    private ICommissionRateService commissionRateService;

    @Autowired
    private IInterestRateService interestRateService;

    private String msg;

    private String error;

    @Transactional(readOnly = true)
    @RequestMapping(value = "admin/segment/getSegmentsList", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    public
    @ResponseBody
    JsonPagedDataAction<SegmentModel> getAccountsList(HttpSession session) {
        JsonPagedDataAction<SegmentModel> data = new JsonPagedDataAction<SegmentModel>() {
        };

        List<SegmentModel> list = new ArrayList<>();
        for (Segment segment : segmentService.findAll()) {
            SegmentModel sm = new SegmentModel();
            sm.setId(segment.getId());
            sm.setName(segment.getName());
            sm.setCommissionRateId(segment.getCommissionRate().getId());
            sm.setInterestRateId(segment.getInterestRate().getId());
//            sm.setCommissionRate(fun);
//            sm.setInterestRate(fun);

            list.add(sm);
        }

        data.setData(list);
        return data;
    }

    @RequestMapping(value = {"/admin/segment/segments"}, method = RequestMethod.GET)
    public ModelAndView showAccounts(HttpSession session) {
        ModelAndView model = new ModelAndView();
        model.addObject("msg", msg);
        model.addObject("error", error);
        msg = "";
        error = "";
        model.setViewName("admin/segment/segments");
        return model;
    }

    @RequestMapping(value = {"/admin/segment/addSegment"}, method = RequestMethod.GET)
    public ModelAndView addSegmentView() {
        ModelAndView model = new ModelAndView();
        model.addObject("segment", new SegmentModel());
        model.setViewName("admin/segment/editSegment");
        return model;
    }

    @RequestMapping(value = {"/admin/segment/editSegment"}, method = RequestMethod.GET)
    public ModelAndView editSegmentView(@RequestParam(value = "id", required = true) Integer id) {
        ModelAndView model = new ModelAndView();
        Segment segment = null;
        if (id != null && id > 0) {
            segment = segmentService.get(id);
        } else {
            error = "Błąd";
            model.setViewName("redirect:/admin/segment/segments");
            return model;
        }
        SegmentModel segmentModel = new SegmentModel();
        segmentModel.setName(segment.getName());
        segmentModel.setId(segment.getId());
        model.addObject("segment", segmentModel);
        model.setViewName("admin/segment/editSegment");
        return model;
    }

    @RequestMapping(value = {"/admin/segment/saveSegment"}, method = RequestMethod.POST)
    public ModelAndView saveSegmnet(@ModelAttribute("segment") SegmentModel segment, BindingResult result, SessionStatus status) {
        ModelAndView model = new ModelAndView();
        Segment segmentToSave = null;
        if (segment.getId() != null && segment.getId() > 0) {
            segmentToSave = segmentService.get(segment.getId());
            if (segmentToSave == null) {
                error = "Niepoprawne dane";
                model.setViewName("redirect:/admin/segment/segments");
                return model;
            }
            segmentToSave.setName(segment.getName());
            segmentService.update(segmentToSave);
            msg = "Typ konta zaktualizowano poprawnie";
        } else {
            segmentToSave = new Segment();
            segmentToSave.setName(segment.getName());
            segmentToSave.setCommissionRate(commissionRateService.create(new CommissionRate()));
            segmentToSave.setInterestRate(interestRateService.create(new InterestRate()));
            segmentService.create(segmentToSave);
            msg = "Typ konta dodano poprawnie";
        }
        model.setViewName("redirect:/admin/segment/segments");
        return model;
    }

    @RequestMapping(value = {"/admin/segment/editInterestRate"}, method = RequestMethod.GET)
    public ModelAndView editInterestRateView(@RequestParam(value = "id", required = true) Integer id) {
        ModelAndView model = new ModelAndView();
        InterestRate interestRate = null;
        if (id != null && id > 0) {
            interestRate = interestRateService.get(id);
        } else if (interestRate == null) {
            error = "Błąd nie ma takiego id";
            model.setViewName("redirect:/admin/segment/segments");
        }

        InterestRateModel rateModel = new InterestRateModel();
        rateModel.setId(interestRate.getId());
        rateModel.setAccountInterest(interestRate.getAccountInterest());

        model.addObject("interestRate", rateModel);
        model.addObject("segment", interestRate.getSegment().getName());
        model.setViewName("admin/segment/editInterestRate");
        return model;
    }

    @RequestMapping(value = {"/admin/segment/editCommissionRate"}, method = RequestMethod.GET)
    public ModelAndView editCommissionRateView(@RequestParam(value = "id", required = true) Integer id) {
        ModelAndView model = new ModelAndView();
        CommissionRate commissionRate = null;
        if (id != null && id > 0) {
            commissionRate = commissionRateService.get(id);
        } else if (commissionRate == null) {
            error = "Błąd nie ma takiego id";
            model.setViewName("redirect:/admin/segment/segments");
        }

        CommissionRateModel rateModel = new CommissionRateModel();
        rateModel.setAccountManagmentFee(commissionRate.getAccountManagmentFee());
        rateModel.setCachePayment(commissionRate.getCachePayment());
        rateModel.setCardFee(commissionRate.getCardFee());
        rateModel.setCashWithdrawal(commissionRate.getCashWithdrawal());
        rateModel.setComfirmationOfTransactionFee(commissionRate.getComfirmationOfTransactionFee());
        rateModel.setExternalTransferFee(commissionRate.getExternalTransferFee());
        rateModel.setiBankFee(commissionRate.getiBankFee());
        rateModel.setId(commissionRate.getId());
        rateModel.setInternalTransferFee(commissionRate.getInternalTransferFee());
        rateModel.setPhoneTransferFee(commissionRate.getPhoneTransferFee());
        rateModel.setZusTransferFee(commissionRate.getZusTransferFee());

        model.addObject("commissionRate", rateModel);
        model.addObject("segment", commissionRate.getSegment().getName());
        model.setViewName("admin/segment/editCommissionRate");
        return model;
    }

    @RequestMapping(value = {"/admin/segment/saveCommissionRate"}, method = RequestMethod.POST)
    public ModelAndView saveSegmnet(@ModelAttribute("commissionRate") CommissionRateModel commissionRate, BindingResult result, SessionStatus status) {
        ModelAndView model = new ModelAndView();
        CommissionRate rate = null;
        if (commissionRate.getId() != null && commissionRate.getId() > 0) {
            rate = commissionRateService.get(commissionRate.getId());
        }
        if (rate != null) {
            rate.setAccountManagmentFee(commissionRate.getAccountManagmentFee());
            rate.setCachePayment(commissionRate.getCachePayment());
            rate.setCardFee(commissionRate.getCardFee());
            rate.setCashWithdrawal(commissionRate.getCashWithdrawal());
            rate.setComfirmationOfTransactionFee(commissionRate.getComfirmationOfTransactionFee());
            rate.setExternalTransferFee(commissionRate.getExternalTransferFee());
            rate.setiBankFee(commissionRate.getiBankFee());
            rate.setInternalTransferFee(commissionRate.getInternalTransferFee());
            rate.setPhoneTransferFee(commissionRate.getPhoneTransferFee());
            rate.setZusTransferFee(commissionRate.getZusTransferFee());
            commissionRateService.update(rate);
            msg = "Poprawnie zaktualizowano";
        } else {
            error = "Blad id";
        }

        model.setViewName("redirect:/admin/segment/segments");
        return model;
    }

    @RequestMapping(value = {"/admin/segment/saveInterestRate"}, method = RequestMethod.POST)
    public ModelAndView saveSegmnet(@ModelAttribute("interestRate") InterestRateModel interestRate, BindingResult result, SessionStatus status) {
        ModelAndView model = new ModelAndView();
        InterestRate rate = null;
        if (interestRate.getId() != null && interestRate.getId() > 0) {
            rate = interestRateService.get(interestRate.getId());
        }
        if (rate != null) {
            rate.setAccountInterest(interestRate.getAccountInterest());
            interestRateService.update(rate);
            msg = "Poprawnie zaktualizowano";
        } else {
            error = "Błąd id";
        }

        model.setViewName("redirect:/admin/segment/segments");
        return model;
    }


}
